package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 平行坐标
 *
 * @author chart
 */
@RestController
@RequestMapping("/plane/chart")
public class PlaneCoordinatesChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;




    //平行坐标系
    @PostMapping("/importData1")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            int k=0;

            //计算出数据有k列
            List<String> list3 = getdata.get(2);
            for (int i = 1; i < list3.size(); i++) {
                String s = list3.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    k++;
                }
            }

            List<String> lows = new ArrayList<>();
            List<String> list4 = getdata.get(getdata.size()-1);
            for (int i = 1; i < list4.size(); i++) {
                String s = list4.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    lows.add(s);
                }
            }


            LinkedHashSet<String> xlist = new LinkedHashSet<>();
            List<String> roomName = new ArrayList<>();



            List<List<List<String>>> newList = new ArrayList<>();
            List<List<String>> list = new ArrayList<>();

            List<Map> maps = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list1 = getdata.get(i);
                if (!CollectionUtils.isEmpty(list1)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < list1.size(); i1++) {
                        String s = list1.get(i1);
                        if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (data.size()==1) {
                        if (i != 1) {
                            newList.add(list);
                            list=new ArrayList<>();
                        }
                        String name = data.get(0);
                        Map map = new HashMap<>();
                        map.put("name",name);
                        maps.add(map);
                        roomName.add(name);
                    }else {
                        List<String> obj1 = new ArrayList<>();
                        for (int i1 = 0; i1 < data.size(); i1++) {
                            String s = data.get(i1);
                            if (i1==0) {
                                xlist.add(s);
                            }else {
                                obj1.add(s);
                            }
                        }
                        list.add(obj1);
                        if (i == getdata.size()-1) {
                            newList.add(list);
                        }
                    }
                }

            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            List<JSONObject> parallelAxis = (List<JSONObject>) jsonObject.get("parallelAxis");
            JSONObject jsonObject1 = parallelAxis.get(0);
            JSONObject jsonObject2 = parallelAxis.get(3);


            List<List<String>> datas = new ArrayList<>();
            for (int i = 0; i < xlist.size()-1; i++) {
                List<String> data = new ArrayList<>();
                datas.add(data);
            }



            List<List<List<String>>> serList = new ArrayList<>();
            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                List<List<String>> serss = new ArrayList<>();
                for (int i1 = 0; i1 < k; i1++) {
                    List<String> list1 = new ArrayList<>();
                    serss.add(list1);
                }

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<String> list1 = lists.get(i1);
                    if (i1 != lists.size()-1) {
                        List<String> datalist = datas.get(i1);
                        datalist.addAll(list1);
                    }

                    for (int i2 = 0; i2 < list1.size(); i2++) {
                        List<String> list2 = serss.get(i2);
                        list2.add(list1.get(i2));
                    }
                }
                serList.add(serss);
            }


            List<Integer> maxs = new ArrayList<>();
            for (int i = 0; i < datas.size(); i++) {
                List<String> list1 = datas.get(i);
                ExcelDataUtils.Result max = ExcelDataUtils.findMinMax(list1);
                maxs.add(max.max);
            }


            ArrayList<JSONObject> newXList = new ArrayList<>();
            ArrayList<String> names = new ArrayList<>();
            Iterator<String> iterator = xlist.iterator();
            while (iterator.hasNext()) {
                    String name = iterator.next();
                    names.add(name);
                }

            for (int i = 0; i < names.size(); i++) {
                String s = names.get(i);
                if (i != names.size()-1) {
                    int max = maxs.get(i);
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.putAll(jsonObject1);
                    jsonObject3.put("name",s);
                    jsonObject3.put("max",max);
                    jsonObject3.put("dim",i);
                    newXList.add(jsonObject3);
                }else {
                    jsonObject2.put("dim",i);
                    jsonObject2.put("name",s);
                    jsonObject2.put("data",lows);
                    newXList.add(jsonObject2);
                }
            }

            jsonObject.put("parallelAxis",newXList);


            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject3 = series.get(0);

            List<JSONObject> serdata =new ArrayList<>();
            for (int i = 0; i < roomName.size(); i++) {
                JSONObject jsonObject4 = new JSONObject();
                jsonObject4.putAll(jsonObject3);

                jsonObject4.put("name",roomName.get(i));
                jsonObject4.put("data",serList.get(i));

                serdata.add(jsonObject4);
            }

            jsonObject.put("series",serdata);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }
}
